package com.rjkj.medicalwaste.repository

import androidx.lifecycle.LiveData
import androidx.paging.PagingSource
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.database.BoxDao
import com.rjkj.medicalwaste.database.GarbageDao
import com.rjkj.medicalwaste.database.UserDao
import com.rjkj.medicalwaste.network.retrofit.ApiInterface
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

class BoxRepository(private val boxDao: BoxDao) {

    suspend fun findBoxByBoxCode(garbageBoxQRCode: String): BoxBean {
        return boxDao.findBoxByBoxOnlyCode(garbageBoxQRCode)
    }

    suspend fun findBoxByTempCode(tempCode: String): TempBoxBean {
        return boxDao.findBoxByTempCode(tempCode)
    }

    suspend fun insertBoxBeanData(data: BoxBean) {
        boxDao.insertBoxBeanData(data)
    }

    suspend fun insertTempBoxBeanData(data: TempBoxBean) {
        boxDao.insertTempBoxData(data)
    }

    fun findAllBoxByRoomId(roomId: String): PagingSource<Int, BoxBean> {
        return boxDao.findAllBoxByRoomId(roomId)
    }

    fun getTempBoxWithGarbageList(roomId: String): PagingSource<Int, TempBoxWithGarbage> {
        return boxDao.getTempBoxWithGarbageList(roomId)
    }

    fun getBoxWithGarbageList(roomId: String,status: String): PagingSource<Int, BoxWithGarbage> {
        return boxDao.getBoxWithGarbageList(roomId,status)
    }

    suspend fun updateBoxOutbound(list: List<BoxDao.BoxRequestStatus>): Int{
        return boxDao.updateBoxOutbound(list)
    }
    suspend fun updateTempBox(tempBoxBean: TempBoxBean): Int {
        return boxDao.updateTempBox(tempBoxBean)
    }

    suspend fun updateTempBoxCount(value: BoxDao.TempBoxRequestCount): Int {
        return boxDao.updateTempBoxCount(value)
    }
    suspend fun updateBoxCount(value: BoxDao.BoxRequestCount): Int {
        return boxDao.updateBoxCount(value)
    }
    companion object {
        @Volatile
        private var instance: BoxRepository? = null
        fun getInstance(boxDao: BoxDao): BoxRepository =
                instance ?: synchronized(this) {
                    instance
                            ?: BoxRepository(boxDao).also {
                                instance = it
                            }
                }

    }
}